<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>实验室管理</title>
    <link rel="stylesheet" href="<%=request.getContextPath()%>/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/static/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">实验室名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" placeholder="请输入实验室名" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

    <div class="layui-card-body">
        <table class="layui-hide" id="demo" lay-filter="test"></table>
        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </script>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
            </div>
        </script>
    </div>
    </div>
</div>


<script type="text/javascript" src="<%=request.getContextPath()%>/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '<%=request.getContextPath()%>/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use('index');
    layui.use(['layer','form', 'table', 'element'], function(){
        var layer = layui.layer //弹层
            ,form = layui.form
            ,table = layui.table //表格
            ,element = layui.element //元素操作
            ,$ = layui.$


        //监听搜索
        form.on('submit(LAY-user-front-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('demo', {
                where: field
            });
        });

        //执行一个 table 实例
        table.render({
             elem: '#demo'
            ,url: '<%=request.getContextPath()%>/laboratory/list' //数据接口
            ,toolbar: '#toolbarDemo' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,cols: [[ //表头
                 {type:'numbers',width:80, title: '序号',fixed: 'center',align: "center"}
                ,{field: 'id', title: 'ID', width:130,align:'center', totalRowText: '合计：'}
                ,{field: 'name', title: '实验室', align:'center'}
                ,{field: 'address', title: '地址',align:'center'}
                ,{fixed: 'right',  align:'center', toolbar: '#barDemo'}
            ]]
            ,page: true //开启分页
        });

        //监听头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'add':
                    layer.open({
                        type: 1 //Page层类型
                        ,btn:["确定","取消"]
                        ,title: '添加实验室'
                        ,skin: 'layui-layer-prompt'
                        ,content: "<div class=''><input type='text' class='layui-layer-input' value='' placeholder='输入实验室'><br/><textarea class='layui-layer-input' style='width:230px;margin-top:3px;' placeholder='实验室地址'></textarea></div>"
                        ,yes: function(index, layero){
                            var name=$(layero).find("input[type='text']").val();
                            var remark=$(layero).find("textarea").val();
                            $.ajax({
                                type: "post",
                                url: "<%=request.getContextPath()%>/laboratory/add",
                                data: {"name":name,"remark":remark},
                                dataType: "json",
                                success: function (date) {
                                    if(date>0){  layer.msg("添加成功");  location.reload();
                                    }else{ layui.msg("添加失败"); }
                                }
                            });
                            layer.close(index);
                        }
                    });
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            if(layEvent === 'detail'){
                layer.open({
                    type: 2,
                    title: '分配老师',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['90%', '90%'],
                    content: '<%=request.getContextPath()%>/laboratory/allocation?id='+data.id //iframe的url
                });
            } else if(layEvent === 'del'){
                layer.confirm('真的删除行么', function(index){
                    layer.close(index);
                    //向服务端发送删除指令
                    $.ajax({
                        type: "post",
                        url: "<%=request.getContextPath()%>/laboratory/del",
                        data: {"id":data.id},
                        dataType: "json",
                        success: function (date) {
                        }
                });
                    layer.msg('删除成功');
                    obj.del(); //删除对应行（tr）的DOM结构
                });
            } else if(layEvent === 'edit'){
                layer.open({
                    type: 1 //Page层类型
                    ,btn:["确定","取消"]
                    ,title: '修改实验室'
                    ,skin: 'layui-layer-prompt'
                    ,content: "<div class=''><input type='text' class='layui-layer-input' value='"+data.name+"' placeholder='实验室名'><br/><textarea class='layui-layer-input' style='width:230px;margin-top:3px;' placeholder='实验室地址'>"+data.address+"</textarea></div>"
                    ,yes: function(index, layero){
                        //按钮【按钮一】的回调
                        var name=$(layero).find("input[type='text']").val();
                        var remark=$(layero).find("textarea").val();
                        $.ajax({
                            type: "post",
                            url: "<%=request.getContextPath()%>/laboratory/update",
                            data: {"id":data.id,"name":name,"remark":remark},
                            dataType: "json",
                            success: function (date) {
                            }
                        });
                        layer.msg("修改成功");
                        location.reload();
                        layer.close(index);
                    }
                });
            }
        });

    });
</script>
</body>
</html>
